package com.hjl.web.servlet;

import com.hjl.domain.Dept_inf;
import com.hjl.domain.PageBean;
import com.hjl.domain.User_inf;
import com.hjl.service.DeptService;
import com.hjl.service.impl.DeptServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 部门表现层
 * @author Lenovo
 * @date 2021/10/8 11:25
 * @version 1.0
 */
@WebServlet("/depts/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();
    String prefix = "/WEB-INF/jsp/dept/";
    String suffix = ".jsp";

    /**跳转WEB-INF里的jsp*/
    public void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "deptlist" + suffix).forward(req, resp);
    }

    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "deptadd" + suffix).forward(req, resp);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "deptedit" + suffix).forward(req, resp);
    }
    /**分页*/
    public void findDeptByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获取数据（当前页码和行数）
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        //默认第1页，5行
        if (currentPage==null || "".equals(currentPage)){
            currentPage = "1";
        }
        if (rows==null || "".equals(rows)){
            rows = "5";
        }
        Map<String, String[]> condition = req.getParameterMap();
        PageBean<Dept_inf> pb = deptService.findDeptByPage(currentPage,rows,condition);
        req.setAttribute("depts",pb);
        req.setAttribute("condition",condition);
        req.getRequestDispatcher("/depts/toList").forward(req,resp);

    }
    /**删除选中*/
    public void delSelected(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        String[] ids = req.getParameterValues("id");
        deptService.delSelect(ids);
        resp.sendRedirect(req.getContextPath()+"/depts/findDeptByPage");
    }
    /**更新*/
    public void updateDept(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        //编码
        req.setCharacterEncoding("utf-8");
        //获取map
        Map<String, String[]> map = req.getParameterMap();
        Dept_inf dept = new Dept_inf();
        try {
            //封装
            BeanUtils.populate(dept,map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
//        调用service方法
        deptService.updateDept(dept);
        //重定向回去
        resp.sendRedirect(req.getContextPath()+"/depts/findDeptByPage");
    }
    /**查询单个（用于编辑）*/
    public void findDept(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        //获取参数
        String id = req.getParameter("id");
        //调用service方法
        Dept_inf dept =   deptService .findDeptById(id);
        //存入request
        req.setAttribute("dept",dept);
        System.out.println(dept);
        //转发到deptedit.jsp
        req.getRequestDispatcher("/depts/toEdit").forward(req,resp);
    }
    /**添加*/
    public void addDept(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
        //设置编码
        req.setCharacterEncoding("utf-8");
        //获得参数
        Map<String, String[]> map = req.getParameterMap();
        //封装对象
        Dept_inf dept = new Dept_inf();
        try {
            BeanUtils.populate(dept,map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service保存
        deptService.addDept(dept);
        //跳转到findDeptByPage
        resp.sendRedirect(req.getContextPath()+"/depts/findDeptByPage");
    }
}
